package customer

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/customer"
	customerReq "counter-help/admin/server/model/customer/request"
)

type CusOrderAssignmentsService struct{}

// CreateCusOrderAssignments 创建cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) CreateCusOrderAssignments(cusOrderAssignments *customer.CusOrderAssignments) (err error) {
	err = global.GVA_DB.Create(cusOrderAssignments).Error
	return err
}

// DeleteCusOrderAssignments 删除cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) DeleteCusOrderAssignments(ID string) (err error) {
	err = global.GVA_DB.Delete(&customer.CusOrderAssignments{}, "id = ?", ID).Error
	return err
}

// DeleteCusOrderAssignmentsByIds 批量删除cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) DeleteCusOrderAssignmentsByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]customer.CusOrderAssignments{}, "id in ?", IDs).Error
	return err
}

// UpdateCusOrderAssignments 更新cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) UpdateCusOrderAssignments(cusOrderAssignments customer.CusOrderAssignments) (err error) {
	err = global.GVA_DB.Model(&customer.CusOrderAssignments{}).Where("id = ?", cusOrderAssignments.ID).Updates(&cusOrderAssignments).Error
	return err
}

// GetCusOrderAssignments 根据ID获取cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) GetCusOrderAssignments(ID string) (cusOrderAssignments customer.CusOrderAssignments, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&cusOrderAssignments).Error
	return
}

// GetCusOrderAssignmentsInfoList 分页获取cusOrderAssignments表记录
// Author [piexlmax](https://github.com/piexlmax)
func (cusOrderAssignmentsService *CusOrderAssignmentsService) GetCusOrderAssignmentsInfoList(info customerReq.CusOrderAssignmentsSearch) (list []customer.CusOrderAssignments, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&customer.CusOrderAssignments{})
	var cusOrderAssignmentss []customer.CusOrderAssignments
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&cusOrderAssignmentss).Error
	return cusOrderAssignmentss, total, err
}

func (s *CusOrderAssignmentsService) GetCusOrderAssignmentsByUserID(userID int) ([]customer.CusOrderAssignments, error) {
	var cusOrderAssignments []customer.CusOrderAssignments
	err := global.GVA_DB.Where("user_id = ?", userID).Find(&cusOrderAssignments).Error
	if err != nil {
		return nil, err
	}
	return cusOrderAssignments, nil
}
